layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery','upload', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        upload = layui.upload,
        admin  = layui.admin;
        var apply = false;

    form.render();
    form.verify({
        business_license: function (value){
            if(typeof value == "undefined" || value == null || value == ""){
                return '营业执照必须传';
            }
        }
    });
    form.on('submit(wxmchInfo_form_apply)',function (data) {

        layer.confirm("确定要提交进件吗", {
            btn: ["确定", "取消"]
        }, function(index, layero){

            $.ajax({
                url:"/wXmchUpgrade/subinfo",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        layer.msg("提交成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 0
                            ,btn: '我知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {
                                layer.closeAll();
                            }
                        })
                    }else{
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }, function(index){

        });


        return false;
    });

    /**
     * oem 新增提交
     */
    form.on('submit(wxmchInfo_form_ok)',function (data) {

        $.ajax({
            url:"/wXmchUpgrade/edit",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("编辑成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });

    $(".face_file").each(function () {

        var userId = $("#userId").val();

        var wxMchId = $("#wxMchId").val();

        var file_btn = $(this).find(".face_file_btn");

        var face_file_img=$(this).find(".face_file_img");

        var fade_file_tip=$(this).find(".fade_file_tip");

        var face_file_input =$(this).find(".face_file_input");

        var keyName =   face_file_input.attr("keyName");

        var img_value =face_file_input.val();

        if(img_value!=null && img_value!="" && img_value!=undefined) {
            var img_url = "/wxapply/getMedia?mediaId=" + img_value;
            face_file_img.attr('src', img_url);
        }

        var  uploadInst =  upload.render({
            elem: file_btn
            ,url: '/wXmchUpgrade/media_upload'
            ,data:{userId:userId,id:wxMchId,keyName:keyName}
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    face_file_img.attr('src', result);
                });
            }
            ,done: function(res){
                if(res.code == 1){
                    face_file_input.val(res.data.media_id);
                    return layer.msg('上传成功');
                }else {
                    face_file_img.attr('src', "");
                    return layer.msg(res.message);
                }
            }
            ,error: function(){
                fade_file_tip.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重新上传</a>');
                fade_file_tip.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    })

});



